package com.kbbywyclient.presenter.base;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;

import com.kbbywyclient.app.API;
import com.kbbywyclient.app.App;
import com.kbbywyclient.app.Key;
import com.kbbywyclient.data.entity.User;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.util.JsonAndObject;
import com.kbbywyclient.util.LogUtil;
import com.kbbywyclient.util.MetaDataUtils;
import com.kbbywyclient.util.OkHttpUtils;
import com.kbbywyclient.util.RSAUtil;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.pre.PreUtils;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * Created by FHW
 * on 2017/3/21
 */

public class BasePresenter {
    private static final SimpleDateFormat TIME_FORMAT=
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE);

    public void bigDataBuried(Map<String,String> params) {
        final Map<String, String> map = getCommonParams();
        if (params != null) {
            map.putAll(params);
        }
        final Observable<String> o =
                Observable.create(subscriber -> {
                    try {
                        OkHttpClient okHttpClient = OkHttpUtils.getOkHttpClient();
                        String logInfo = JsonAndObject.object2Json(map);
                        LogUtil.d("大数据埋点参数:" + logInfo);
                        RequestBody body = new FormBody.Builder().add("logInfo",
                                RSAUtil.encryptByPublicKey(logInfo, Key.publicKey)).
                                build();
                        Request request = new Request.Builder().
                                url(API.BIG_DATA_BURIED_URL).
                                post(body).build();
                        Response response = okHttpClient.newCall(request).execute();
                        if (response.isSuccessful()) {
                            subscriber.onNext(response.body().string());
                            subscriber.onCompleted();
                        } else {
                            subscriber.onError(new RuntimeException("post bigData error,code:" + response.code()));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        subscriber.onError(e);
                    }
                });
        ApiServiceHelper.
                appointThreadNormal(o).
                subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(String s) {
                        LogUtil.d("post big data result:" + s);
                    }
                });
    }

    /**
     * 获取大数据埋点公共参数
     */
    private Map<String,String> getCommonParams(){
        Context context= App.getInstance();
        Map<String,String> params=new HashMap<>();
        User user=PreUtils.getUser(context);
        params.put("id_user",user.getSalesID()==-1?"":String.valueOf(user.getSalesID()));
        params.put("id_other", StringUtil.getDeviceId(context));
        params.put("city", TextUtils.isEmpty(user.getCityName())?"":user.getCityName());
        long current=System.currentTimeMillis();
        params.put("temporary_id",StringUtil.getDeviceId(context)+current);
        params.put("time",TIME_FORMAT.format(current));
        params.put("client","android");//
        params.put("origin", MetaDataUtils.getMetaDataString(context,"UMENG_CHANNEL"));
        params.put("model", Build.BRAND+"_"+Build.MODEL);//手机型号
        params.put("system",Build.VERSION.RELEASE);//手机操作系统
        return params;
    }
}
